package com.xizi.forum.service.impl;

import com.xizi.forum.common.AppResult;
import com.xizi.forum.dao.BoardInfoMapper;
import com.xizi.forum.model.BoardInfo;
import com.xizi.forum.service.IBoardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BoardInfoServiceImpl implements IBoardInfoService {

    @Autowired
    private BoardInfoMapper boardInfoMapper;

    @Value("${xizi-forum.index.board-num:9}")
    private int num;

    /**
     * 获取版块链表
     * @return
     */
    @Override
    public AppResult<List<BoardInfo>> selectListNum() {
        List<BoardInfo> boardInfoList = boardInfoMapper.selectListNum(num);
        return AppResult.success(boardInfoList);
    }

    // 改变 板块帖子数量
    @Override
    public int updateBoardArticleCount(Long boardId,Boolean check) {
        BoardInfo boardInfo = new BoardInfo();
        Integer boardArticleCount = boardInfoMapper.selectByPrimaryKey(boardId).getArticleCount();
        boardInfo.setId(boardId);
        if(check == true){
            boardInfo.setArticleCount(boardArticleCount + 1);
        }else {
            boardInfo.setArticleCount(boardArticleCount - 1);
        }
        int count = boardInfoMapper.updateByPrimaryKeySelective(boardInfo);
        return count;
    }

    /**
     * 获取其中一个版块
     * @param id 版块id
     * @return
     * @throws Exception
     */
    @Override
    public BoardInfo getBoardInfoById(Long id) {
        BoardInfo boardInfo = boardInfoMapper.selectByPrimaryKey(id);
        return boardInfo;
    }
}
